﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_F4MaterialKosei : Form
    {
        public string ID;
        public frm_Edit_F4MaterialKosei()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.ID = null;
            getCompList();
            //getPartList();
            getUnitID();
            getGroupList();

            //this.robotNo.LostFocus += robotNo_LostFocus;
            //this.partNo.LostFocus += partNo_LostFocus;
        }
        /*
        void partNo_LostFocus(object sender, EventArgs e)
        {
            ComboboxItem item = new ComboboxItem();
            for (int i = 0; i < this.partNo.Properties.Items.Count; i++)
            {
                item = (ComboboxItem)this.partNo.Properties.Items[i];
                if (item.Value.ToString().Trim() == this.partNo.Text.Trim())
                {
                    this.partNo.SelectedIndex = i;
                }
            }
        }

        void robotNo_LostFocus(object sender, EventArgs e)
        {
            ComboboxItem item = new ComboboxItem();
            for (int i = 0; i < this.robotNo.Properties.Items.Count; i++)
            {
                item = (ComboboxItem)this.robotNo.Properties.Items[i];
                if (item.Value.ToString().Trim() == this.robotNo.Text.Trim())
                {
                    this.robotNo.SelectedIndex = i;
                }
            }
        }
        */
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(ID))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT * FROM [231_Kosei] WHERE ID=" + this.ID;
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        for (int i = 0; i < this.robotNo.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.robotNo.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RobotNo"].ToString())
                            {
                                this.robotNo.SelectedIndex = i;
                            }
                        }
                        
                        for (int i = 0; i < this.partNo.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.partNo.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["PartNo"].ToString())
                            {
                                this.partNo.SelectedIndex = i;
                            }
                        }
                                                

                        this.quantityKosei.Text = dataReader["Quantity"].ToString();

                        for (int i = 0; i < this.groupID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.groupID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Group"].ToString())
                            {
                                this.groupID.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.unitID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.unitID.Properties.Items[i];
                            if (item.Value.ToString().ToUpper() == dataReader["Unit"].ToString().ToUpper())
                            {
                                this.unitID.SelectedIndex = i;
                            }
                        }
                    }
                    dataReader.Close();
                    sql_cmd.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }
            return ret;
        }
        
        /*
        ////////////////////////////////////////////////
        /// Load Part list to form
        ////////////////////////////////////////////////
        private void getPartList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT PartNo"
                    + " FROM [222_Part] ORDER BY PartNo";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {                    
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["PartNo"].ToString().Trim();                    
                    item.Value = dataReader["PartNo"].ToString().Trim();
                    //this.robotNo.Properties.Items.Add(item);
                    this.partNo.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Part list");
            }
        }
        */
        ////////////////////////////////////////////////
        /// Load Comp list to form
        ////////////////////////////////////////////////
        private void getCompList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT PartNo FROM [222_Part] ORDER BY PartNo";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["PartNo"].ToString().Trim();
                    item.Value = dataReader["PartNo"].ToString().Trim();
                    this.robotNo.Properties.Items.Add(item);
                    this.partNo.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Comp list");
            }
        }

        ////////////////////////////////////////////////
        /// Load Unit ID
        ////////////////////////////////////////////////
        private void getUnitID()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT UnitID"
                    + " FROM [222_Unit] ORDER BY UnitID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["UnitID"].ToString().Trim();
                    item.Value = dataReader["UnitID"].ToString().Trim();
                    this.unitID.Properties.Items.Add(item);                    
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Unit list");
            }
        }
 
        /// <summary>
        /// Load Group List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void getGroupList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Group] FROM [231_Kosei]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["Group"].ToString().Trim();
                    item.Value = dataReader["Group"].ToString().Trim();
                    this.groupID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Group list");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }        
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;
            string robot = null, part = null, unit = null, group = null;
            float qty = 0;

            ComboboxItem item = new ComboboxItem();

            for (int i = 0; i < this.robotNo.Properties.Items.Count; i++)
            {
                item = (ComboboxItem)this.robotNo.Properties.Items[i];
                if (item.Value.ToString().Trim() == this.robotNo.Text.Trim())
                {
                    this.robotNo.SelectedIndex = i;
                }
            }
            
            for (int i = 0; i < this.partNo.Properties.Items.Count; i++)
            {
                item = (ComboboxItem)this.partNo.Properties.Items[i];
                if (item.Value.ToString().Trim() == this.partNo.Text.Trim())
                {
                    this.partNo.SelectedIndex = i;
                }
            }

            if (this.robotNo.SelectedIndex < 0)
            {
                err = "Please select Robot No.";
            }
            else
            {
                //robot = this.robotNo.Text.Trim();
                item = (ComboboxItem)this.robotNo.SelectedItem;
                robot = item.Value.ToString();
            }

            if (string.IsNullOrEmpty(err))
            {
                if (this.partNo.SelectedIndex < 0)
                {
                    err = "Please select Part No.";
                }
                else
                {
                    //part = this.partNo.Text.Trim();
                    item = (ComboboxItem)this.partNo.SelectedItem;
                    part = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.quantityKosei.Text.Trim()))
                {
                    err = "Please enter Quantity";
                }
                else
                {
                    if (float.TryParse(this.quantityKosei.Text.Trim(), out qty))
                    {
                        if (qty < 0)
                        {
                            err = "Quantity must be greater than 0";
                        }
                    }
                    else
                    {
                        err = "Invalid quantity";
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (this.groupID.SelectedIndex < 0)
                {
                    err = "Please select Group";
                }
                else
                {
                    item = (ComboboxItem)this.groupID.SelectedItem;
                    group = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (this.unitID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.unitID.SelectedItem;
                    unit = item.Value.ToString();
                }

                bool conflict = false;

                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT COUNT(ID) FROM [231_Kosei] WHERE [RobotNo]='" + robot + "'"
                        + " AND [PartNo]='" + part + "' AND [Group]='" + group + "' AND ID <>" + this.ID;
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        if (dataReader[0] != DBNull.Value)
                        {
                            if (Int32.Parse(dataReader[0].ToString()) > 0)
                            {
                                conflict = true;
                            }
                        }
                    }
                    dataReader.Close();
                    sql_cmd.Dispose();

                    if (conflict)
                    {
                        err = "This configuration existed.";
                    }
                    else
                    {
                        sql_cmd = myConnection.CreateCommand();
                        sql_cmd.CommandText = "UPDATE [231_Kosei] SET [RobotNo]='" + robot + "', [PartNo]='" + part + "',"
                            + " [Quantity]=" + qty + ", [Group]='" + group + "', [Unit]='" + unit + "'"
                            + " WHERE ID=" + this.ID;
                        sql_cmd.ExecuteNonQuery();
                        sql_cmd.Dispose();
                        err = "Material Kosei was updated succeedfully.";
                    }
                    myConnection.Close();
                    MessageBox.Show(err);
                    this.Dispose();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show("Cannot update material kosei");
                }
            }
            else
            {
                MessageBox.Show(err);
            }
        }
    }
}
